<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions 
 * @package    Communityad
 * @copyright  Copyright 2009-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AdminWidgetsController.php 2011-02-16 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Communityad_AdminWidgetsController extends Core_Controller_Action_Admin {

  protected $_createWidgetMsg;

  // Function: 'Manage Block Tab' Show the listing of widget settings which set by admin.
  public function manageAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('communityad_admin_main', array(), 'communityad_admin_widget_setting');
    $page = $this->_getParam('page', 1); // Page id: Controll pagination.
    $this->view->page_id = $this->_getParam('page_id', 0);
    $this->view->check = $this->_getParam('check', 0);
    if (!empty($this->_createWidgetMsg)) {
      $this->view->success_message = $this->_createWidgetMsg;
      $this->_createWidgetMsg = 0;
    }
    $sortingColumnName = $this->_getParam('idSorting', 0);
    $pagesettingsTable = Engine_Api::_()->getItemTable('communityad_pagesetting');
    $pagesettingsTableName = $pagesettingsTable->info('name');
    $coreTable = Engine_Api::_()->getDbTable('pages', 'core');
    $coreName = $coreTable->info('name');
    $corecontentTable = Engine_Api::_()->getDbTable('content', 'core');
    $corecontentName = $corecontentTable->info('name');
    // Queary: For finding the info of widgets settings.
    // Left Join - 1: For finding the 'Page Title' from 'core_page' table.
    // Left Join - 2: For finding the 'widget title' from 'core_content' table.
    $pagesettingsSelect = $pagesettingsTable->select()
            ->setIntegrityCheck(false)
            ->from($pagesettingsTableName)
            ->joinLeft($coreName, "$coreName.page_id = $pagesettingsTableName.page_id", array('displayname'))
            ->joinLeft($corecontentName, "$corecontentName.content_id = $pagesettingsTableName.widget_content_id");
    if (empty($sortingColumnName)) {
      $pagesettingsSelect->order($pagesettingsTableName . '.pagesetting_id DESC');
      $this->view->id_orderby = 0;
    } else {
      $pagesettingsSelect->order($pagesettingsTableName . '.pagesetting_id ASC');
      $this->view->id_orderby = 1;
    }

    $this->view->paginator = Zend_Paginator::factory($pagesettingsSelect);

    $this->view->paginator->setItemCountPerPage(20);
    $this->view->paginator->setCurrentPageNumber($page);
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();

      foreach ($values as $key => $value) {
        if ($key == 'delete_' . $value) {
          $pagesettingTable = Engine_Api::_()->getItem('communityad_pagesetting', $value);
          if (!empty($pagesettingTable)) {
            Engine_Api::_()->getDbtable('content', 'core')->delete(array('content_id =?' => $pagesettingTable->widget_content_id));
            Engine_Api::_()->getDbtable('pagesettings', 'communityad')->delete(array('pagesetting_id =?' => $value));
          }
        }
      }
    }
  }

  // Function: Create Manage Block.
  public function widgetsettingsAction() {

    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('communityad_admin_main', array(), 'communityad_admin_widget_setting');

    $this->view->form = $form = new Communityad_Form_Admin_Widgets();
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $page_id = 0;
      $widgetContentId = 0;
      $widgetContentId = 0;
      $values = $form->getValues();
      $is_edit = $this->_getParam('edit', 0);
      if (!empty($is_edit)) {
        $values['page_id'] = $this->_getParam('page_id', 0);
        $values['ad_widget_id'] = $this->_getParam('widget_id', 0);
      }

      if (!empty($values)) {
        $is_error = 0;
        // check for empty(zero) values  List_comment_widgets
        if ($values['ad_widget_value'] == 0) {
          $is_error = 1;
        }

        // IF limit is zero then show error.
        if ($is_error == 1) {
          $error = $this->view->translate('Limit can not be zero !');
          $this->view->status = false;
          $error = Zend_Registry::get('Zend_Translate')->_($error);

          $form->getDecorator('errors')->setOption('escape', false);
          $form->addError($error);
          return;
        }

        $adWidgetId = $values['ad_widget_id'];
        // Function Calling: For finding widget info like 'Widget Title', 'Widget Name', 'Widget Position' by pasing the 'ID' of widgets.
        $adWidgetInfoArray = Engine_Api::_()->getDbtable('pagesettings', 'communityad')->adwidgetInfo($adWidgetId);
        // Condition: If 'Check box of widgets creation selected' or page is none widgetized.
        if (!empty($values['ad_widget_confirm']) || $values['page_id'] == 'none') {
					$ads_ajax_based = $values['ajax_enabled'];
          $ads_value = $values['ad_widget_value'];
          $ad_block_title = $values['ad_block_title'];
          // Condition: If widget info is for 'None widgetized page - widget' then would not be save widget in 'core_content' table, only save data in 'communityad_pagesettings'
          if ($adWidgetInfoArray['position'] != 'static') {
            $title = '{"title":"' . $adWidgetInfoArray['title'] . '"}';
            // Info: About widget which will place in widgetized page.
            $page_id = $values['page_id'];
            $containerPosition = $adWidgetInfoArray['position'];
            // Condition: If page have 'Full Width' maybe that can be 'top' or 'bottom' then set  that in variable acording to the page else set 'main'.
            if ($containerPosition == 'top' || $containerPosition == 'bottom') {
              $mainContainerName = $containerPosition;
              $containerPosition = 'middle';
            } else {
              $mainContainerName = 'main';
            }
            $widgetName = $adWidgetInfoArray['name'];

            // Coding for: Bydefault when save data then insert widget in the selected position.
            $contentTable = Engine_Api::_()->getDbtable('content', 'core');
            $contentTableName = $contentTable->info('name');
            // Queary: Find out the main container name, may be that container for 'Full Width' or other position.
            $selectContainer = $contentTable->select()
                    ->from($contentTableName, array('content_id'))
                    ->where('page_id = ?', $page_id)
                    ->where('name = ?', $mainContainerName)
                    ->where('type = ?', 'container')
                    ->limit(1);
            $fetchContent = $selectContainer->query()->fetchAll();
            if (!empty($fetchContent)) {
              // Condition: Find out the 'content_id' of position where widget will placed.
              $container_id = $fetchContent[0]['content_id'];
              $selectPosition = $contentTable->select()
                      ->from($contentTableName, array('content_id'))
                      ->where('parent_content_id = ?', $container_id)
                      ->where('type = ?', 'container')
                      ->where('name = ?', $containerPosition)
                      ->limit(1);
              $fetchContent = $selectPosition->query()->fetchAll();
              if (!empty($fetchContent)) {
                // Queary: Check the widget that, widget should not be already placed in selected position..
                $pagePositionId = $fetchContent[0]['content_id'];
                $IsWidgetSelect = $contentTable->select()
                        ->from($contentTableName, array('content_id'))
                        ->where('page_id = ?', $page_id)
                        ->where('type = ?', 'widget')
                        ->where('name = ?', $widgetName)
                        ->where('parent_content_id = ?', $pagePositionId);
                $IsWudgetEnabled = $IsWidgetSelect->query()->fetchAll();
                if (empty($IsWudgetEnabled)) {
                  // Queary: Insert the widget in given position.
                  $widgetDisplay = $contentTable->createRow();
                  $widgetDisplay->page_id = $page_id;
                  $widgetDisplay->type = 'widget';
                  $widgetDisplay->name = $widgetName;
                  $widgetDisplay->parent_content_id = $pagePositionId;
                  $widgetDisplay->order = 999;
                  $widgetDisplay->params = $title;
                  $widgetDisplay->save();
                  $widgetContentId = $widgetDisplay->content_id;
                } else {
                  $widgetContentId = $IsWudgetEnabled[0]['content_id'];
                }
              }
            }
          }
        } else {
          $page_id = $values['page_id'];
          $ad_block_title = $values['ad_block_title'];
          $adWidgetId = $values['ad_widget_id'];
          $ads_value = $values['ad_widget_value'];
					$ads_ajax_based = $values['ajax_enabled'];
        }
				include_once(APPLICATION_PATH ."/application/modules/Communityad/controllers/license/license2.php");
      }
      $this->_createWidgetMsg = 1;
      $this->_helper->redirector->gotoRoute(array('module' => 'communityad', 'controller' => 'widgets', 'action' => 'manage', 'page_id' => $values['page_id'], 'check' => $values['ad_widget_confirm']), 'admin_default', true);
    }
  }

	// Function: When enable or disable ajax based ads for a particular block
  public function ajaxadsenableAction() {
    $pagesetting_id = $this->_getParam('pagsettingId');
    $db = Engine_Db_Table::getDefaultAdapter();
    $db->beginTransaction();
    try {
      $pagesettings = Engine_Api::_()->getItem('communityad_pagesetting', $pagesetting_id);
      $pagesettings->ajax_enabled = !$pagesettings->ajax_enabled;
      $pagesettings->save();
      $db->commit();
    } catch (Exception $e) {
      $db->rollBack();
      throw $e;
    }
    $this->_redirect('admin/communityad/widgets/manage');
  }

  // Function: Ad-report from report admin tab.
  public function adreportsAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('communityad_admin_main', array(), 'communityad_admin_adreports');
    $sortingColumnName = $this->_getParam('idSorting', 0);
    $page = $this->_getParam('page', 1); // Page id: Controll pagination.
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      if (!empty($values)) {
        foreach ($values as $key => $value) {
          if ($key == 'delete_' . $value) {
            Engine_Api::_()->getDbtable('adcancels', 'communityad')->update(array('is_cancel' => 1), array('adcancel_id =?' => $value));
          }
        }
      }
    }
    $this->view->success_message = $successMessages = $this->_getParam('form_status'); // Status that success message would be show or not.
    $ad_cancel_table = Engine_Api::_()->getItemTable('communityad_adcancel');
    $ad_cancel_name = $ad_cancel_table->info('name');
    $userTable = Engine_Api::_()->getItemTable('user');
    $userName = $userTable->info('name');
    $useradTable = Engine_Api::_()->getItemTable('userads');
    $useradName = $useradTable->info('name');

    $ad_cancel_select = $ad_cancel_table->select()
            ->setIntegrityCheck(false)
            ->from($userName, array('displayname'))
            ->join($ad_cancel_name, "$userName.user_id = $ad_cancel_name.user_id")
            ->join($useradName, "$useradName.userad_id = $ad_cancel_name.ad_id", array('cads_title'))
            ->where($ad_cancel_name . '.is_cancel =?', 0);
    if (empty($sortingColumnName)) {
      $ad_cancel_select->order($ad_cancel_name . '.adcancel_id DESC');
      $this->view->id_orderby = 0;
    } else {
      $ad_cancel_select->order($ad_cancel_name . '.adcancel_id ASC');
      $this->view->id_orderby = 1;
    }		
    $this->view->paginator = Zend_Paginator::factory($ad_cancel_select);
    $this->view->paginator->setItemCountPerPage(10);
    $this->view->paginator->setCurrentPageNumber($page);
  }

  public function reportdiscriptionAction() {
    $reportsId = $this->_getParam('reportId');
    $this->view->ad_cancel_dis = Engine_Api::_()->getItem('communityad_adcancel', $reportsId)->report_description;
  }

  // Function: When delete any block from 'Manage Ads Block Tab'.
  public function deleteAction() {
    $pagesetting_id = $this->_getParam('pagsettingId');
    $this->view->pagesetting_id = $pagesetting_id;
    // Check post
    if ($this->getRequest()->isPost()) {
      $pagesettingTable = Engine_Api::_()->getItem('communityad_pagesetting', $pagesetting_id);
      if (!empty($pagesettingTable)) {
        Engine_Api::_()->getDbtable('content', 'core')->delete(array('content_id =?' => $pagesettingTable->widget_content_id));
        Engine_Api::_()->getDbtable('pagesettings', 'communityad')->delete(array('pagesetting_id =?' => $pagesetting_id));
      }
      $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => 10,
              'parentRefresh' => 10,
              'messages' => array($this->view->translate('Block settings deleted Successsfully.'))
      ));
    }
  }

  public function moduleDeleteAction() {
    $pagesetting_id = $this->_getParam('module_id');
    $this->view->pagesetting_id = $pagesetting_id;
    // Check post
    if ($this->getRequest()->isPost()) {
      $pagesettingTable = Engine_Api::_()->getItem('communityad_module', $pagesetting_id)->delete();
      $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => 10,
              'parentRefresh' => 10,
              'messages' => array($this->view->translate('Module settings deleted Successsfully.'))
      ));
    }
  }

  public function deletereportsAction() {
    $pagesetting_id = $this->_getParam('reportId');
    $this->view->pagesetting_id = $pagesetting_id;
    // Check post
    if ($this->getRequest()->isPost()) {
      Engine_Api::_()->getDbtable('adcancels', 'communityad')->update(array('is_cancel' => 1), array('adcancel_id =?' => $pagesetting_id));
      $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => 10,
              'parentRefresh' => 10,
              'messages' => array($this->view->translate('Report deleted Successsfully.'))
      ));
    }
  }

  public function noneWidgetCodeAction() {
    $adWidgetId = $this->_getParam('adWidgetId');
    $this->view->none_widget_code_div = $widgetCode = Engine_Api::_()->getDbtable('pagesettings', 'communityad')->widgetCode($adWidgetId, 1);
    $this->view->none_widget_code_nodiv = $widgetCode = Engine_Api::_()->getDbtable('pagesettings', 'communityad')->widgetCode($adWidgetId, 0);
  }

  public function adCreateAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('communityad_admin_main', array(), 'communityad_admin_module_manage');

    $this->view->form = $form = new Communityad_Form_Admin_Adcreate();
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      if (!empty($values)) {
        $serializedArray = serialize($values['auth_module']);
        Engine_Api::_()->getApi('settings', 'core')->setSetting('communityad_adcreate', $serializedArray);
      }
    }
  }

  public function admoduleManageAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('communityad_admin_main', array(), 'communityad_admin_admodule_manage');
    $this->view->enabled_modules_array = Engine_Api::_()->getDbtable('modules', 'core')->getEnabledModuleNames();
    $page = $this->_getParam('page', 1); // Page id: Controll pagination.
    $pagesettingsTable = Engine_Api::_()->getItemTable('communityad_module');
    $pagesettingsTableName = $pagesettingsTable->info('name');
    $pagesettingsSelect = $pagesettingsTable->select();
    $this->view->paginator = Zend_Paginator::factory($pagesettingsSelect);
    $this->view->paginator->setItemCountPerPage(20);
    $this->view->paginator->setCurrentPageNumber($page);
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values as $key => $value) {
        if ($key == 'delete_' . $value) {
          $obj = Engine_Api::_()->getItem('communityad_module', $value);
          if (empty($obj->is_delete)) {
            $obj->delete();
          }
        }
      }
    }
  }

  // Function: Manage Module - Creation Tab.
  public function admoduleCreateAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('communityad_admin_main', array(), 'communityad_admin_admodule_manage');
    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
    $module_table = Engine_Api::_()->getDbTable('modules', 'communityad');
    $module_name = $module_table->info('name');
    $this->view->form = $form = new Communityad_Form_Admin_Module();
		$this->view->modules_id = $this->_getParam('module_id', 0);
		$this->view->module_form_count = count($form->admodule_name->options);
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      //$values = $this->getRequest()->getPost();
			$values = $form->getValues();
      if (!empty($values)) {
				$itemType = '';
//				$displayable = array();
//				if( !empty($values['displayable']) ) {
//					$displayable = $values['displayable'];
//					$displayable = array_sum($displayable);
//				}
        $moduleName = $values['admodule_name'];
        $moduleTitle = $values['addbtable_title'];
        $itemType = $values['adtable_name'];
        $field_title = $values['adtable_title'];
        $field_body = $values['adtable_body'];
        //$field_image = $values['adtable_photo'];
        $field_owner = $values['adtable_owner'];
				if( !empty($itemType) ) {
					$hasItemType = Engine_Api::_()->hasItemType( $itemType );
				}
        if (!empty($hasItemType)) {
          $table_name = Engine_Api::_()->getItemTable($itemType)->info('name');

          // Condition: Check owner field is available or not in given table.
          if (!empty($field_owner)) {
            $is_owner = $db->query("SHOW COLUMNS FROM " . $table_name . " LIKE '" . $field_owner . "'")->fetch();
            if (empty($is_owner)) {
              $error_owner = $this->view->translate('Please check the Content Owner Field. A field matching the one specified by you could not be found in the database table.');
            }
          } else {
            $is_owner = 1;
          }

          // Condition: Check title field is available or not in given table.
          if (!empty($field_title)) {
            $is_title = $db->query("SHOW COLUMNS FROM " . $table_name . " LIKE '" . $field_title . "'")->fetch();
            if (empty($is_title)) {
              $error_title = $this->view->translate('Please check the Content Title Field. A field matching the one specified by you could not be found in the database table.');
            }
          } else {
            $is_title = 1;
          }

          // Condition: Check body field is available or not in given table.
          if (!empty($field_body)) {
            $is_body = $db->query("SHOW COLUMNS FROM " . $table_name . " LIKE '" . $field_body . "'")->fetch();
            if (empty($is_body)) {
              $error_body = $this->view->translate('Please check the Content Body Field. A field matching the one specified by you could not be found in the database table.');
            }
          } else {
            $is_body = 1;
          }

          // Condition: Check image field is available or not in given table.
          if (!empty($field_image)) {
            $is_image = $db->query("SHOW COLUMNS FROM " . $table_name . " LIKE '" . $field_image . "'")->fetch();
            if (empty($is_image)) {
              $error_image = $this->view->translate('Please check the Content Image Field. A field matching the one specified by you could not be found in the database table. If this content does not have an image field, then leave this blank. Ad creators will be able to upload their own image.');
            }
          } else {
            $is_image = 1;
          }
					include_once(APPLICATION_PATH ."/application/modules/Communityad/controllers/license/license2.php");

        } else {
          $itemError = $this->view->translate('Please enter a correct database table item.');
        }
      }
    }
    // Show the error.
    if (!empty($itemError) || !empty($error_title) || !empty($error_body) || !empty($error_image) || !empty($error_owner)) {
      $this->view->status = false;

      if (!empty($error_owner)) {
        $error_owner = Zend_Registry::get('Zend_Translate')->_($error_owner);
        $form->getDecorator('errors')->setOption('escape', false);
        $form->addError($error_owner);
      }
      if (!empty($itemError)) {
        $itemError = Zend_Registry::get('Zend_Translate')->_($itemError);
        $form->getDecorator('errors')->setOption('escape', false);
        $form->addError($itemError);
      }
      if (!empty($error_title)) {
        $error_title = Zend_Registry::get('Zend_Translate')->_($error_title);
        $form->getDecorator('errors')->setOption('escape', false);
        $form->addError($error_title);
      }
      if (!empty($error_body)) {
        $error_body = Zend_Registry::get('Zend_Translate')->_($error_body);
        $form->getDecorator('errors')->setOption('escape', false);
        $form->addError($error_body);
      }
      if (!empty($error_image)) {
        $error_image = Zend_Registry::get('Zend_Translate')->_($error_image);
        $form->getDecorator('errors')->setOption('escape', false);
        $form->addError($error_image);
      }
      return;
    }
  }

  // Function: Calling from ( Ajax ): When admin select any module from drop down from 'manage modules tab' then return the table name.
  public function admodulesinfoAction() {
    $admodule = $this->_getParam('module_name');
    $table_name = 0;
    $title_field = 0;
    $owner_field = 0;
    $body_field = 0;
    //$image_field = 0;
    $module_table = Engine_Api::_()->getItemTable('communityad_module');
    $module_name = $module_table->info('name');
    $select = $module_table->select()
            ->from($module_name, array('table_name', 'title_field', 'body_field', 'owner_field'))
            ->where('module_name =?', $admodule);
    $admodules = $select->query()->fetchAll();
    if (!empty($admodules)) {
      $table_name = $admodules[0]['table_name'];
      $title_field = $admodules[0]['title_field'];
      $body_field = $admodules[0]['body_field'];
      $owner_field = $admodules[0]['owner_field'];
    }

    $this->view->table_name = $table_name;
    $this->view->title_field = $title_field;
    $this->view->body_field = $body_field;
    $this->view->owner_field = $owner_field;
  }

	public function footermsgAction(){ }

}